<template>
	<div class="login-container" :style="{backgroundImage:'url('+require('@/assets/timg.png')+')'}">
		<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="login-form">
			<el-form-item label="用户名" prop="username">
				<el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="确认密码" prop="password">
				<el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
				<el-button @click="resetForm('ruleForm')">重置</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import axios from 'axios'
	export default {
		data() {
			var checkName = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('名字不能为空'));
				} else {
					callback();
				}

			};
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					return callback(new Error('请输入密码'));
				} else {
					callback();
				}
			};
			return {
				ruleForm: {
					username: "zs",
					password: "123456"

				},
				rules: {
					password: [{
						validator: validatePass,
						trigger: 'blur'
					}],

					username: [{
						validator: checkName,
						trigger: 'blur'
					}]
				}
			};

		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						// 开始请求后台看是否能够登录，如果可以就跳转到about 不可以就直接提示登录失败
						axios.get("http://localhost:8081/t-user/login", {
								params: this.ruleForm
							})
							.then(response=> {
							    // 处理成功情况
							  if(response.data.code==200){
								  this.$router.push({name:"about"});// 跳转到about页面
							  }else{
								  alert("失败啦！！！");
							  }
							  })
							  .catch((error)=> {
							    // 处理错误情况
							    console.log(error);
							  })
							  .then(()=> {
							    // 总是会执行
							  });
							
						//this.$router.push({name:"about"});
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}

	}
</script>

<style>
	.login-form {
		width: 350px;
		margin: 160px auto;
		/* 上下间距160px，左右自动居中*/
		background-color: rgb(255, 255, 255, 0.8);
		/* 透明背景色 */
		padding: 30px;
		border-radius: 20px;
		/* 圆角 */
	}

	/* 背景 */

	.login-container {
		position: absolute;
		width: 100%;
		height: 100%;
		/* background: url("~@/assets/timg.png"); */
	}

	/* 标题 */
	.login-title {
		color: #303133;
		text-align: center;
	}
</style>
